package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.TestModel;
import cn.com.yx.ms.service.IServerInfoService;
import cn.com.yx.ms.service.ITestService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.configuration.BaseConstant;
import cn.yx.framework.security.Des3Kit;
import cn.yx.framework.security.MD5Util;
import cn.yx.framework.security.RSAKit;
import cn.yx.framework.system.SystemUtil;
import cn.yx.framework.util.DateUtil;
import cn.yx.framework.view.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "servers")
@Api(value = "服务器管理", description = "用于服务器管理的操作接口")
public class ServerInfoController {

    private static final Logger logger = LoggerFactory.getLogger(ServerInfoController.class);

    @Resource
    private IServerInfoService iServerInfoService;

    // region << 服务器信息 >>

    @RequestMapping("zip")
    @ApiOperation("导出服务器包")
    public void exportServerInfoZip(HttpServletResponse res) throws Exception {
        iServerInfoService.exportInfo(res);
    }

    @ResponseBody
    @RequestMapping(value = "this", method = RequestMethod.GET)
    @ApiOperation("获取服务器信息数据")
    public ApiResult findOneServer() throws Exception {
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                iServerInfoService.findThisServer());
    }

    // endregion

    // region << 临时测试 >>
    @RequestMapping(value = "/testDownload", method = RequestMethod.GET)
    public void testDownload(HttpServletResponse res) {
        String fileName = "1.txt";
        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = res.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(new File("d://"
                    + fileName)));
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("success");
    }
    // endregion


}